define(function(require, exports, module) {
    var template = require("template");
    var Ajax = require("ajax");
    var layer = require("layer");
    var pagination = require("js/plugins/page/jquery.pagination.js");
    require('jquery');
    
    var util = {
        CutOff :function() {       //截断过长文字
            // var length_2 = null,length_num2, text = null;
            // $('#monitor_table>tbody>tr>td:nth-child(2)>a').each(function(){
            //     length_num2 = null;
            //     var html = $(this).text();
            //     var length_num = $(this).width();
            //     text =  '<span style="display:none">' + $(this).text() + '</span>';
            //     $(this).append(text);
            //     length_num2 = $(this).children('span').width();
            //     $(this).children('span').empty();
            //     if(length_num2>length_num) {
            //         var n = Math.floor(length_num/16);
            //         var str = html.substring(0,n-3);
            //         str = str + "...";
            //         $(this).html(str);
            //     }
            // });
        },
//        Pages :function(){
//            $('span[name="go"]').click(function(){
//                var word = $('.cssTxt').val();
//                if(isNaN(word)) {
//                    layer.tips("请输入数字",'.cssTxt');
//                }
//            })
//        },
        Collection: function(info_type,Index) {    //收藏
            var t =this;
            var arr =[];
            var selectNums = [],selectNum =null,label = null;
            //全选//取消
            $('.checkAll').click(function(){
                selectNums =[];
                $('.checkboxChange').addClass('checked').html('&#xe669;');
                $('.checkItem').parents('tr').css('background','rgba(78,84,108,0.04)');             //样式
                $('.checkItem').prop('checked',true);
                $('.checkItem').each(function(){
                    $(this).parents('tr').addClass('allCollect');
                    if($(this).parents('tr').children().eq(Index).children().attr('class') == 'iconfont collectionItem noCollect') {
                        selectNums.push($(this).attr('data-id'));
                    }
                });
                selectNum = selectNums.join(',');
                $(this).css('display','none');
                $(this).next().css('display','inline');
                if(selectNums.length>0) {
                    $('.page_footer>:last-child>span').removeClass('disabledBtn_1').addClass('collectionAll');    //按钮状态切换
                }
            });
            $('.delCheckAll').click(function(){
                $('.checkboxChange').removeClass('checked').html('&#xe6dc;');
                $('.checkItem').parents('tr').css('background','#fff');                                        //样式
                $('.checkItem').prop('checked',false);
                $('.checkItem').parents('tr').removeClass('allCollect');
                selectNums.length = 0;
                $(this).css('display','none');
                $(this).prev().css('display','inline');
                $('.page_footer>:last-child>span').removeClass('collectionAll').addClass('disabledBtn_1');    //按钮状态切换
            });
            //单个收藏
            $('.monitor_table>tbody').delegate('span','click',function(){
                //区分点击多选框和点击星星
                $('.checkboxChange').removeClass('checked').html('&#xe6dc;');
                $('.checkItem').prop('checked',false);
                $('.checkItem').parents('tr').removeClass('allCollect');
                $('.checkAll').css('display','inline');
                $('.delCheckAll').css('display','none');
                //
                selectNum = null;
                $('.collectionLabel').empty();
                selectNum = $(this).attr('data-id');
                if ($(this).attr('class') == 'iconfont collectionItem noCollect') {
                    //加唯一标识
                    $(this).parents('tr').addClass('allCollect');
                    //遮罩层
                    $('.mask').show();
                    $('.collectionWindowBg').css('z-index','99999').css('position','absolute');
                    $('.collectionWindow').css('display','block');
                    //每次点击清空输入框
                    $('.collectionWindow_one>input').val('');
                    Ajax.ajax({
                        url: localhost + '/userMonitor/SelectLabelByCreator.do',
                        data: {info_type: info_type},
                        success: function(data){
                            for (var i =0; i<data.returnData.length; i++) {
                                var html = '<li><span>'+ data.returnData[i].label +'</span></li>';
                                $('.collectionLabel').append(html);
                            }
                        }
                    })
                } else {
                    $(this).parents('tr').addClass('allDelete');
                    $(this).parents('tr').siblings().removeClass('allDelete');
                    var $this = $(this);
                    layer.open({
                        content: '是否取消该收藏？',
                        btn: ['取消', '确定'], //按钮
                        yes:function(){
                            $this.parents('tr').removeClass('allDelete');
                            layer.close(layer.index);
                        },
                        btn2: function () {
                            Ajax.ajax({
                                url: localhost + '/userMonitor/deleteCollect.do',
                                data: {info_ids: selectNum},
                                success: function (data) {
                                    if (data.returnCode == 10000) {
                                        layer.msg(data.returnMessage, {icon: 1, time: 2000}, function () {
                                            $('.allDelete').children().children('span').html('&#xe607;');
                                            $this.removeClass('isCollect').addClass('noCollect');
                                            // self.location.reload();
                                        })
                                    } else {
                                        layer.msg(data.returnMessage, {icon: 5, time: 2000}, function () {
                                    
                                        })
                                    }
                                }
                            });
                        }
                    })
                }
            });
            //批量收藏
            // $('#monitor_table>tbody').delegate('input','click',function(){
            $('.monitor_table>tbody').delegate('i','click',function(){
                //字体图标事件
                if($(this).attr('class') == 'iconfont checkboxChange') {
                    $(this).addClass('checked').html('&#xe669;');
                    $(this).next().prop("checked",true);
                    $(this).parents('tr').css('background','rgba(78,84,108,0.04)');             //样式
                } else{
                    $(this).removeClass('checked').html('&#xe6dc;');
                    $(this).next().prop("checked",false);
                    $(this).parents('tr').css('background','#fff');                               //样式
                }
                //清空数组
                selectNums.length = 0;
                $('.checkItem').each(function(){
                    if($(this).prop('checked') == true){
                        if($(this).parents('tr').children().eq(Index).children().attr('class') == 'iconfont collectionItem noCollect') {
                            selectNums.push($(this).attr('data-id'));
                        }
                        $(this).parents('tr').addClass('allCollect');
                    } else {
                        $(this).parents('tr').removeClass('allCollect');
                    }
                });
                if(selectNums.length>0) {
                    $('.page_footer>:last-child>span').removeClass('disabledBtn_1').addClass('collectionAll');
                } else {
                    $('.page_footer>:last-child>span').removeClass('collectionAll').addClass('disabledBtn_1');
                }
                selectNum = selectNums.join(',');
            });
            $('.page_footer>:last-child>span').click(function(){
                if($(this).attr('class') == 'disabledBtn_1'){
                    return;
                }
                $('.collectionLabel').empty();
                if(selectNums.length == 0) {
                    // layer.msg('您还未选择需要收藏的信息',{icon: 5, time: 1000});
                } else {
                    //遮罩层
                    $('.collectionWindowBg').css('z-index','99999').css('position','absolute');
                    $('.collectionWindow').css('display','block');
                    $('.mask').show();
                    //每次点击清空输入框
                    $('.collectionWindow_one>input').val('');
                    Ajax.ajax({
                        url: localhost + '/userMonitor/SelectLabelByCreator.do',
                        data: {info_type: info_type},
                        dataType: 'json',
                        success: function(data){
                            for (var i =0; i<data.returnData.length; i++) {
                                var html = '<li><span>'+ data.returnData[i].label +'</span></li>';
                                $('.collectionLabel').append(html);
                            }
                        }
                    })
                }
            });
            //点击标签
            $('.collectionLabel').delegate('li','click',function(){
                var labelOld = $('.collectionWindow_one>input').val();
                var last = labelOld[labelOld.length-1];
                if(labelOld !== '' && last!== ',' && last!== '，') {
                    labelOld = labelOld + ',';
                }
                arr = $('.collectionWindow_one>input').val().split(',');
                label = $(this).children('span').html();
                if(!($.inArray(label, arr) == -1)) {
                    return;
                }
                // var html = '<li><i class="iconfont delLabel">&#xe650;</i><span for="">' +label+ '</span></li>';
                // $('.choose_label').append(html);
                
                $('.collectionWindow_one>input').val(labelOld + label+",");
            });
            //输入框内标签事件
            $('.choose_label').delegate('i','click',function(){
                $(this).parents('li').remove();
            });
            var count = 0;
            //取消
            $('.collectionCancelBtn').click(function(){
                $('.collectionWindowBg').css('position','static');
                $('.collectionWindow').css('display','none');
                $('.mask').hide();
                count=0;
            });
            //提交
            selectNum = selectNums.join(',');
            $('.collectionSubmitBtn').click(function(){
                count++;
                if(count ==1) {
                    var str = $('.collectionWindow_one>input').val();
                    if(str.indexOf("，")){
                        str = str.replace("，",",");
                        if(str[str.length] == ',') {
                            str = str.substring(0,str.length-1); //去除尾部逗号
                        }
                    }
                    str = str.split(',');
                    for(var i=0;i<str.length;i++) {
                        if(str[i].length >= 10) {
                           str[i] = str[i].slice(0,10);
                        }
                    }
                    str = str.join(',');
                    Ajax.ajax({
                        url: localhost + '/userMonitor/LabelCollect.do',
                        type:'post',
                        dataType: 'json',
                        data: {info_id: selectNum,labels: str,info_type:info_type},
                        success: function (data) {
                            if (data.returnCode == 10000) {
                                layer.msg(data.returnMessage,{icon: 1, time: 1000},function(){
                                    $('.collectionWindowBg').css('background','rgba(78,84,108,0.4)').css('position','static');
                                    $('.collectionWindow').css('display','none');
                                    $('.mask').hide();
                                    $('.allCollect').children().children('span').html('&#xe675;');
                                    $('.allCollect').children().children('span').removeClass('noCollect');
                                    $('.allCollect').children().children('span').addClass('isCollect');
                                    // self.location.reload();
                                    count=0;
                                    selectNum=null;
                                    $('.page_footer>:last-child>span').removeClass('collectionAll').addClass('disabledBtn_1');
                                })
                            } else {
                                layer.msg(data.returnMessage,{icon: 5, time: 1000},function(){
                                    $('.collectionWindowBg').css('background','rgba(78,84,108,0.4)').css('position','static');
                                    $('.collectionWindow').css('display','none');
                                    $('.mask').hide();
                                })
                            }
                        }
                    });
                }
            });
        },
        Cut: function(KewWord){
            var n = KewWord.length;
            if(n>15) {
                KewWord = KewWord.substring(0,15);
                KewWord = KewWord + "...";
                return KewWord;
            }
            return KewWord;
        }
    };
    module.exports = util;
});